package cn.kgc.web.service;

import cn.kgc.common.entity.Article;
import cn.kgc.common.entity.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * Created by teacher ZHANG on 2023/2/6
 */
@Service
public class AuthorServiceImpl implements AuthorService {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${rest.author.url}")
    private String authorUrl;

    @Override
    public List<Author> findAuthor(List<Integer> authorIds) {
        StringBuilder builder = new StringBuilder();

        for (Integer authorId : authorIds) {
            if (builder.length() > 0) {
                builder.append(",");
            }

            builder.append(authorId);
        }

        Author[] authors = restTemplate.getForObject(authorUrl + "/" + builder.toString(), Author[].class);

        return Arrays.asList(authors);
    }
}
